package com.itheima.mapper.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BrandMapperImpl implements BrandMapper {

    /**
     * 查询所有的品牌
     */
    @Override
    public List<Brand> selectAll() {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.调用接口中方法
        List<Brand> brands = brandMapper.selectAll();
        //4.关闭会话
        sqlSession.close();
        //5.返回结果
        return brands;
    }

    @Override
    public void add(Brand brand) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.调用接口中方法
        brandMapper.add(brand);
        //4.关闭会话
        sqlSession.commit();  //要手动提交
        sqlSession.close();
    }

    @Override
    public Brand selectById(Integer id) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.调用接口中方法
        Brand brand = brandMapper.selectById(id);
        //4.关闭会话
        sqlSession.close();
        //5.返回结果
        return brand;
    }

    @Override
    public void update(Brand brand) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.调用接口中方法
        brandMapper.update(brand);
        //4.关闭会话
        sqlSession.commit();  //要手动提交
        sqlSession.close();
    }

    @Override
    public void deleteById(Integer id) {
        //1.获取会话对象
        SqlSession sqlSession = MyBatisUtils.getSession();
        //2.获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.调用接口中方法
        brandMapper.deleteById(id);
        //4.关闭会话
        sqlSession.commit();  //要手动提交
        sqlSession.close();
    }
}
